package com.tan.kotlintest.rxjava;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class Client {

    public static void main(String[] args) {

        //-------------------------RxJava中Observable 和 Observer基本使用----------------
        //1.创建一个被观察者
        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) {
                e.onNext("发射器发送数据");
                e.onComplete();
            }
        });

        //2.创建观察者
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("观察者订阅了被观察者 ");
            }

            @Override
            public void onNext(String s) {
                System.out.println("观察者观察到了数据: " + s);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("观察者onError: ");
            }

            @Override
            public void onComplete() {
                System.out.println("观察者onComplete: ");
            }
        };
        //3. 观察者 订阅  被观察者
        observable.subscribe(observer);
    }

}
